<template>
    <div>
        <el-form :model="shopForm"  ref="shopForm" label-width="100px" style="width:460px">
            <el-form-item label="所属市场：" prop="marketId">
				<el-select v-model="shopForm.marketId" placeholder="请选择市场">
					<el-option v-for="(item,index) in store.marketOptions" :key="item.marketId" :label="item.marketName" :value="item.marketId"></el-option>
				</el-select>
			</el-form-item>
            <el-form-item label="店铺名称:" prop="shopName">
                    <el-input v-model="shopForm.shopName"></el-input>
            </el-form-item>
            <el-form-item label="店铺电话:" prop="shopMobile">
                    <el-input v-model="shopForm.shopMobile"></el-input>
            </el-form-item>
            <el-form-item label="店铺地址:" prop="shopAddress">
                    <el-input v-model="shopForm.shopAddress"></el-input>
            </el-form-item>
             <el-form-item label="负责人电话:" prop="userMobile">
                    <el-input v-model="shopForm.userMobile" @change="checkMobile()"></el-input>
            </el-form-item>
            <el-form-item label="负责人姓名:" prop="userName">
                    <el-input v-model="shopForm.userName"></el-input>
            </el-form-item>
            <el-form-item label="负责人身份证:" prop="userNo">
                    <el-input v-model="shopForm.userNo"></el-input>
            </el-form-item>
              <el-form-item label="法人电话:" prop="shopLegalMobile">
                    <el-input v-model="shopForm.shopLegalMobile"></el-input>
            </el-form-item>
              <el-form-item label="法人代表姓名:" prop="shopLegalName">
                    <el-input v-model="shopForm.shopLegalName"></el-input>
            </el-form-item>
              <el-form-item label="法人身份证:" prop="shopLegalNo">
                    <el-input v-model="shopForm.shopLegalNo"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit()">{{buttonName}}</el-button>
                <!-- <button type="submit" class="el-button el-button--primary">{{buttonName}}</button> -->
                <el-button @click="turnToList">返回列表</el-button>
                <el-button @click="resetForm('shopForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
export default {
    data() {
        return {
          num:0,
          buttonName:'',
          shopId: this.$route.params.id,
          shopForm:{
              shopName: '',
              shopMobile: '',
              shopAddress: '',
              userMobile: '',
              userName: '',
              userNo:'',
              shopLegalMobile:'',
              shopLegalName:'',
              shopLegalNo:'',
              marketId: '',
              userState: 1,
              userType: 40,
              shopState: 1

          },
          store:{
            marketOptions:[],
            params:{
                pageSize:10,
                pageIndex:1,
                marketId:this.$marketId,
            },
        },
        };
    },
     watch:{
        '$route'(){
            console.log(this.$route.params.id)
            if (!this.$route.params.id){
                this.resetForm('shopForm')
                this.buttonName = '立即创建'
            }
        }
    },

    created(){
       this.$store.commit('getMarketOptions',this.store)
       if (this.shopId != null){
             this.getData();
           this.buttonName = "立即更新"
       }else{
           this.buttonName = "立即创建"

       }
    },
    methods: {
        assignFn(shopEditForm,userEditForm){
            this.num ++
            if(this.num==2){
                Object.assign(this.shopForm,shopEditForm,userEditForm)
                console.log(this.shopForm)
            }
        },
        getData(){
           let  shopEditForm = {}
           let  userEditForm = {}
           this.$axios.get('/controller/getShopList' ,
                { params: {shopId: this.shopId}}).then((res) => {
                    console.log(res.data.result[0])
                    shopEditForm = res.data.result[0]
                    delete  shopEditForm.shopCreateTime;
                    delete  shopEditForm.shopUpdateTime;
                    delete  shopEditForm.market;
                    this.assignFn(shopEditForm,userEditForm)
                })
            this.$axios.get('/controller/getUserList' ,
                { params: {shopId: this.shopId, userType: 40}}).then((res) => {
                    console.log(res.data.result[0])
                    userEditForm = res.data.result[0]
                delete  userEditForm.userCreateTime;
                delete  userEditForm.userUpdateTime;
                delete  userEditForm.shop;
                delete  userEditForm.market;
                this.assignFn(shopEditForm,userEditForm)
            })

        },
        turnToList(){
            this.$router.push('/shop/shopList')
        },
        checkMobile(){
            this.$axios.get('/controller/selectUserByMobile',{params: {userMobile: this.shopForm.userMobile, userState: 1 }})
    		.then((res)=>{
                if (res.data.code !=1){
                  this.$message({
                    message: res.data.msg,
                    type: 'error'
                  });
                  this.shopForm.userMobile = ''
                }
            })
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        onSubmit() {
            var requestParam = this.shopForm
            var formData = new FormData();
            for(let i in requestParam){
               formData.append(i,requestParam[i])
            }
          	this.$axios({
                method: 'post',
                url: '/controller/insertOrUpdateShop',
                data: formData
                }).then((res) => {
                if (res.data.code == 1){
                    this.$message({
                        message: res.data.msg,
                        type: 'success'
                    });
                    this.$router.push('/shop/shopList')
                }else{
                    this.$message({
                        message: res.data.msg,
                        type: 'error'
                    });
                    this.$router.push('/shop/shopForm')
                }
        })

        },
    }
}
</script>
